package com.xh.tools;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.StringJoiner;

/**
 * @author xh
 * @description 字符串工具类
 */
public class StringUtils {

    public static final String PATTERN_NUMBER = "^[-+]?(0|[1-9]\\d*)(\\.\\d+)?$";
    public static final String PATTERN_INTEGER = "^[-+]?\\d+$";
    public static final String COMMA = ",";

    public static boolean isNull(String str) {
        return str == null;
    }

    public static boolean isNotNull(String str) {
        return !isNull(str);
    }

    public static boolean isBlank(String str) {
        return str == null || str.trim().isEmpty();
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    /**
     * 判断字符串是否匹配指定模式
     *
     * @param str     字符串
     * @param pattern 模式
     * @return 匹配结果
     */
    public static boolean isPattern(String str, String pattern) {
        if (isNull(str)) {
            return false;
        }
        return str.matches(pattern);
    }

    /**
     * 判断字符串是否为数字
     *
     * @param str 字符串
     * @return 是否为数字
     */
    public static boolean isNumber(String str) {
        return isPattern(str, PATTERN_NUMBER);
    }

    /**
     * 判断字符串是否为整数
     *
     * @param str 字符串
     * @return 是否为整数
     */
    public static boolean isInteger(String str) {
        return isPattern(str, PATTERN_INTEGER);
    }

    /**
     * 将字符串用分隔符分隔并封装为List
     *
     * @param strs      字符串
     * @param delimiter 分隔符
     * @return 分隔后的List对象
     */
    public static List<String> str2List(String strs, String delimiter) {
        if (isNull(strs)) {
            return Collections.emptyList();
        }
        if (isNull(delimiter)) {
            return Collections.singletonList(strs);
        }
        return Arrays.asList(strs.split(delimiter));
    }

    /**
     * 字符串按,分隔并封装为List
     *
     * @param strs 字符串
     * @return List
     */
    public static List<String> str2List(String strs) {
        return str2List(strs, COMMA);
    }

    /**
     * List对象转换成字符串
     *
     * @param list      List对象
     * @param delimiter 分隔符
     * @return 字符串
     */
    public static String list2Str(List<String> list, String delimiter) {
        if (list == null || list.isEmpty()) {
            return "";
        }
        if (isNull(delimiter)) {
            delimiter = "";
        }
        StringJoiner joiner = new StringJoiner(delimiter);
        list.forEach(joiner::add);
        return joiner.toString();
    }

    /**
     * List对象转换成字符串,默认分隔符为","
     *
     * @param list 列表
     * @return 字符串
     */
    public static String list2Str(List<String> list) {
        return list2Str(list, COMMA);
    }

    public static void main(String[] args) {
        System.out.println(md5("1"));
    }

    /**
     * 字符串转MD5，并格式化为32位
     *
     * @param str 待转换的字符串
     * @return MD5字符串
     */
    public static String md5(String str) {
        if (isBlank(str)) {
            return "";
        }
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] digest = md5.digest(str.getBytes());

            BigInteger bigInteger = new BigInteger(1, digest);
            StringBuilder string = new StringBuilder(bigInteger.toString(16));
            while (string.length() < 32) {
                string.insert(0, "0");
            }
            return string.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(String.format("字符串[%s]转md5失败：%s", str, e.getMessage()));
        }
    }
}
